package com.android.component;

import com.android.component.CoreService.MyBinder;
import com.utils.HT_Log;
import com.utils.LogUtil;

import android.R.bool;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;

public class ServiceTest extends CommonTest {

    protected static final String TAG = "ServiceTest";

    boolean service_connected = false;
    MyBinder binder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setButton("start service", onClickListener);
        setButton("stop service", onClickListener);

        setButton("bind service", onClickListener);
        setButton("unbind service", onClickListener);

        setButton("call binder function", onClickListener);
    }

    OnClickListener onClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(ServiceTest.this, CoreService.class);

            if (v.getId() == btns[0].getId()) {
                startService(i);
                for (int j = 0; j < 20; j++) {
                    HT_Log.e(TAG, "lll" + j);
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            } else if (v.getId() == btns[1].getId()) {
                stopService(i);
            } else if (v.getId() == btns[2].getId()) {
                bindService(i, service_connection, Context.BIND_AUTO_CREATE);
            } else if (v.getId() == btns[3].getId()) {
                if (service_connected) {
                    unbindService(service_connection);
                } else {
                    LogUtil.i(TAG, "service not connected");
                }
            } else if (v.getId() == btns[4].getId()) {
                if (service_connected) {
                    new Thread() {
                        public void run() {
                            LogUtil.i(TAG, "doWork in thread:" + Thread.currentThread().getId());
                            binder.getService().doWork();
                        }
                    }.start();
                } else {
                    LogUtil.i(TAG, "service not connected");
                }
            }
        }
    };

    ServiceConnection service_connection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            LogUtil.d(TAG, "onServiceConnected");
            service_connected = true;
            binder = (MyBinder) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            service_connected = false;
            LogUtil.d(TAG, "onServiceDisconnected");
        }
    };

}
